package cn.tjie.order.service.impl;

import cn.tjie.basic.util.LoginContext;
import cn.tjie.basic.util.PageList;
import cn.tjie.order.domain.OrderPetAcquisition;
import cn.tjie.order.dto.OrderDto;
import cn.tjie.order.mapper.OrderPetAcquisitionMapper;
import cn.tjie.order.query.OrderPetAcquisitionQuery;
import cn.tjie.order.service.IOrderPetAcquisitionService;
import cn.tjie.basic.service.impl.BaseServiceImpl;
import cn.tjie.org.domain.Employee;
import cn.tjie.org.domain.Shop;
import cn.tjie.org.mapper.EmployeeMapper;
import cn.tjie.org.mapper.ShopMapper;
import cn.tjie.pet.domain.Pet;
import cn.tjie.pet.domain.SearchMasterMsg;
import cn.tjie.pet.mapper.PetMapper;
import cn.tjie.pet.mapper.SearchMasterMsgMapper;
import cn.tjie.user.domain.Logininfo;
import cn.tjie.user.domain.User;
import cn.tjie.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //先使用工具类从请求里面获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取employee
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        if(employee.getShop_id()!=null){
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if(shop.getAdminId()!=employee.getId()){
                //店员
                query.setEmpId(employee.getId());
            }
            //是店铺管理员
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    /**
     * 确认订单
     * @param orderDto
     */
    @Override
    @Transactional
    public void confirm(OrderDto orderDto) {

        //点击确定订单，处理人去用户那里把宠物接回来-平台就会多一个宠物，需要录入
        BigDecimal money = orderDto.getMoney();
        Integer paytype = orderDto.getPaytype();
        //获取宠物的发布信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        //1.生成宠物对象
        Pet pet = msg2Pet(msg,money);
        petMapper.save(pet);
        //2.修改订单状态为1，并且修改金额，金额是最终成交价
        order.setState(1);
        order.setPaytype(orderDto.getPaytype());
        order.setPetId(pet.getId());
        order.setPrice(orderDto.getMoney());
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主消息的状态为2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
    }

    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setSaleprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    @Transactional
    public void cancel(Long id) {
    //1.点击取消，将寻主消息放入寻主池：state=3，shopId=null
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //将寻主消息放入寻主池--reject就把state=3，shopId=NULL
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //改收购订单的状态为state=-1
        order.setState(-1);
        //更新
        orderPetAcquisitionMapper.update(order);
    }


}
